package com.codi.api.controller;

import com.codi.base.log.annotation.Loggable;
import com.codi.base.util.EqualsUtil;
import com.codi.bus.core.domain.User;
import com.codi.bus.core.fund.domain.FundDividendChange;
import com.codi.bus.core.fund.enums.FundDividendChangeBizTypeEnum;
import com.codi.bus.core.fund.request.DividendChangeRequestDto;
import com.codi.bus.core.fund.request.MyDividendHistoryRequestDto;
import com.codi.bus.core.fund.service.FundDividendChangeService;
import com.codi.bus.core.fund.service.FundDividendService;
import com.codi.bus.core.job.FundBasicJobService;
import com.codi.bus.core.resultModel.BaseResult;
import com.codi.bus.core.service.QueryService;
import com.codi.bus.core.service.UserService;
import com.codi.fcloud.ufx.hssale.query.TradeApplyQuery;
import com.codi.trade.dto.TradeApplyQryDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 模块名
 *
 * @author shi.pengyan
 * @version 1.0 2017-10-23 9:12
 * @since 1.0
 */
@Slf4j
@RestController
@RequestMapping("/_refresh")
public class RefreshController {

    @Autowired
    private UserService userService;

    @Autowired
    private FundBasicJobService fundBasicJobService;

    @Autowired
    private FundDividendService fundDividendService;

    @Autowired
    private FundDividendChangeService fundDividendChangeService;

    @Autowired
    private QueryService queryService;

    @Autowired
    private TradeApplyQuery tradeApplyQuery;


    private static final String DEFAULT_T = "helloworld";

    /**
     * org acct 状态
     * @param t
     * @return
     */
    @Loggable
    @GetMapping("/org_state")
    public BaseResult refreshOrgAcctState(@RequestParam(defaultValue = "123") String t) {

        if (EqualsUtil.equals(t, DEFAULT_T)) {
            userService.checkOrgAcctStateAndSMS();
        }
        return new BaseResult();
    }

    /**
     * 刷新推荐的基金
     *
     * @param t
     * @return
     */
    @Loggable
    @GetMapping("/recommend_fund")
    public BaseResult refreshRecommendFund(@RequestParam(defaultValue = "123") String t) {
        if (EqualsUtil.equals(t, DEFAULT_T)) {
            fundBasicJobService.refreshRecommendedFund();
        }
        return new BaseResult();
    }

    /**
     * 刷新T0列表
     *
     * @param t
     * @return
     */
    @Loggable
    @GetMapping("/t0_order_list")
    public BaseResult refreshT0OrderList(@RequestParam(defaultValue = "123") String t) {
        if (EqualsUtil.equals(t, DEFAULT_T)) {
            fundBasicJobService.refreshT0SortList();
        }
        return new BaseResult();
    }

    //test
    @GetMapping("/test/fund/dividend/my")
    public BaseResult myFundDividend(MyDividendHistoryRequestDto dto) {
        return fundDividendService.queryMyDividendHistory(dto);
    }

    @Loggable
    @PostMapping("/test/fund/dividend/change")
    public BaseResult changeDividend(DividendChangeRequestDto dto) {
        dto.setBizType(FundDividendChangeBizTypeEnum.COMMON.getKey());
        return fundDividendService.change(dto);
    }

    @Loggable
    @PostMapping("/test/fund/dividend/portfolio/change")
    public BaseResult changePortfolioDividend(FundDividendChange dto) {
        fundDividendChangeService.addPortfolioApplyWait(dto);

        BaseResult result = new BaseResult(true);
        return result;
    }

    @Loggable
    @GetMapping("/test/fund/dividend/portfolio/change/refresh")
    public BaseResult changePortfolioDividendRefresh() {
        fundDividendChangeService.applyPortfolioChange();

        BaseResult result = new BaseResult(true);
        return result;
    }

    @Loggable
    @GetMapping("/test/asset/my")
    public BaseResult getMyAsset(@RequestParam("userId") Long userId) {
        return queryService.queryAssets(userId);
    }

    @Loggable
    @GetMapping("/test/applys")
    public BaseResult getApplys(@RequestParam("userId") Long userId,
                                @RequestParam("requestNum") Integer requestNum,
                                @RequestParam("queryFlag") Integer queryFlag,
                                @RequestParam("beginNum") Integer beginNum) {
        return queryService.queryApplys(userId, requestNum, queryFlag, beginNum, "1", true);
    }

    @Loggable
    @GetMapping("/test/applys/all")
    public Map<String, Object> getPlainApplys(@RequestParam("userId") Long userId,
                                              @RequestParam("requestNum") Integer requestNum,
                                              @RequestParam("beginNum") Integer beginNum) {
        User user = userService.getUser(userId);

        TradeApplyQryDto dto = TradeApplyQryDto.builder()
                                               .request_num(requestNum.toString())
                                               .qry_beginrownum(beginNum.toString())
                                               .client_id(user.getExternalClientId())
                                               .build();
        return tradeApplyQuery.query(dto);
    }

}
